﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public static class MouseEvent 
{
    public static Vector3 Position
    {
        get
        {
#if UNITY_EDITOR
            return Input.mousePosition;
#else
		        if (Input.touchCount > 0)
		        {
		            return Input.GetTouch(0).position;
		        }
		        return Vector3.zero;
#endif
        }
    }
    public static bool IsPointDown
    {
        get
        {
#if UNITY_EDITOR
            return Input.GetMouseButtonDown(0);
#else
		        if(Input.touchCount > 0)
		        {
		            if (Input.touches[0].phase == TouchPhase.Began)
		                return true;
		        }
		        return false;
#endif
        }
    }
    public static bool IsPointMove
    {
        get
        {
#if UNITY_EDITOR
            return Input.GetMouseButton(0);
#else
		        if (Input.touchCount > 0)
		        {
		            //if (Input.GetTouch(0).phase == TouchPhase.Moved)
		                return true;
		        }
		        return false;
#endif
        }
    }
    public static bool IsPointUp
    {
        get
        {
#if UNITY_EDITOR
            return Input.GetMouseButtonUp(0);
#else
		        if (Input.touchCount > 0)
		        {
		            if (Input.GetTouch(0).phase == TouchPhase.Canceled || Input.GetTouch(0).phase == TouchPhase.Ended)
		                return true;
		        }
		        return false;
#endif
        }
    }

    private static List<RaycastResult> _guiObjectRaycastResult;
    private static PointerEventData _guiEventData;
    public static bool IsTouchedGUIObjects()
    {
        if (_guiEventData == null)
        {
            _guiEventData = new PointerEventData(EventSystem.current);
            _guiObjectRaycastResult = new List<RaycastResult>();
        }
        _guiEventData.pressPosition = Input.mousePosition;
        _guiEventData.position = Input.mousePosition;
        EventSystem.current.RaycastAll(_guiEventData, _guiObjectRaycastResult);
        return _guiObjectRaycastResult.Count > 0;
    }
}
